package com.ruoyi.supervise.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.OpinionCauseContent;
import com.ruoyi.supervise.domain.ProReviewPassRefuse;
import com.ruoyi.supervise.domain.ProsOpinionInfo;
import com.ruoyi.supervise.domain.SurveyResults;
import com.ruoyi.supervise.domain.SurveyResultsApplyInfo;
import com.ruoyi.supervise.mapper.SurveyResultsMapper;
import com.ruoyi.supervise.service.ISurveyResultsService;
import com.ruoyi.supervise.util.XmlReadUtil;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

/**
 * @ClassName: SurveyResultsImpl
 * @Description: 勘察成果审查serviceimpl
 * @Author wtj
 * @DateTime 2020年3月16日 上午9:12:34
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class SurveyResultsImpl implements ISurveyResultsService 
{
    @Autowired
    private SurveyResultsMapper surveyResultsMapper;

	@Override
	public List<SurveyResults> selectSurveyResultsList(SurveyResults surveyResults) {
		if(StringUtils.isEmpty(surveyResults.getBeginReportDate()) && !StringUtils.isEmpty(surveyResults.getEndReportDate())) {
			surveyResults.setBeginReportDate("1970-01-01");
		}else if(!StringUtils.isEmpty(surveyResults.getBeginReportDate()) && StringUtils.isEmpty(surveyResults.getEndReportDate())) {
			surveyResults.setEndReportDate(DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN));
		}
		if(StringUtils.isEmpty(surveyResults.getBeginAcceptedYear()) && !StringUtils.isEmpty(surveyResults.getEndAcceptedYear())) {
			surveyResults.setBeginReportDate("1970-01-01");
		}else if(!StringUtils.isEmpty(surveyResults.getBeginAcceptedYear()) && StringUtils.isEmpty(surveyResults.getEndAcceptedYear())) {
			surveyResults.setEndAcceptedYear(DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN));
		}
		return surveyResultsMapper.selectSurveyResultsList(surveyResults);
	}

	@Override
	public SurveyResultsApplyInfo selectSurveyResultsApplyInfoByGuid(String guid) {
		// TODO Auto-generated method stub
		return surveyResultsMapper.selectSurveyResultsApplyInfoByGuid(guid);
	}

	@Override
	public List<ProsOpinionInfo> selectProsOpinionInfoByDataGuid(String dataGuid) {
		List<ProsOpinionInfo> poiList = surveyResultsMapper.selectProsOpinionInfoByDataGuid(dataGuid);
		for(ProsOpinionInfo poi:poiList) {
			if("上报".equals(poi.getRole())) {
				poi.setCauseContent("0");
			}else if("程序性审查通过".equals(poi.getRole()) || "程序性审查退回".equals(poi.getRole())) {
//				ProsOpinionInfo pros = new ProsOpinionInfo();
//				pros.setDataGuid(dataGuid);
//				pros.setPreviousStateGuid(poi.getGuid());
//				pros.setRole(poi.getRole());
//				ProsOpinionInfo prosa = surveyResultsMapper.selectProsOpinionInfoByPreviousStateGuid(pros);
				ProsOpinionInfo prosa = surveyResultsMapper.selectProsOpinionInfoByGuid(poi.getPreviousStateGuid());
				if(!StringUtils.isNull(prosa) && !StringUtils.isEmpty(prosa.getOperateDate()) && !StringUtils.isEmpty(poi.getOperateDate())) {
					poi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(prosa.getOperateDate()), DateUtil.parseDateTime(poi.getOperateDate()), DateUnit.DAY)));
				}else {
					poi.setCauseContent("0");
				}
			}else if("整改上报".equals(poi.getRole())) {
				ProsOpinionInfo pros = new ProsOpinionInfo();
				pros.setDataGuid(dataGuid);
				//pros.setRole("指定审查人员");
				pros.setRole("程序性审查退回");
				pros.setCheckCount(poi.getCheckCount());
				ProsOpinionInfo prosa = surveyResultsMapper.selectProsOpinionInfoByPreviousStateGuid(pros);
				if(!StringUtils.isNull(prosa) && !StringUtils.isEmpty(prosa.getOperateDate()) && !StringUtils.isEmpty(poi.getOperateDate())) {
					poi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(prosa.getOperateDate()), DateUtil.parseDateTime(poi.getOperateDate()), DateUnit.DAY)));
				}else {
					poi.setCauseContent("0");
				}
			}else if("指定审查人员".equals(poi.getRole())) {
				ProsOpinionInfo result = rectReportReturn(poiList,poi);
				if(StringUtils.isNotNull(result)) {
					poi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(result.getOperateDate()), DateUtil.parseDateTime(poi.getOperateDate()), DateUnit.DAY)));
				}else {
					poi.setCauseContent("0");
				}
			}else if("审查人员复审".equals(poi.getRole()) || "审查人员初审".equals(poi.getRole())) {
				ProsOpinionInfo pros = new ProsOpinionInfo();
				pros.setDataGuid(dataGuid);
				pros.setRole("指定审查人员");
				pros.setCheckCount(poi.getCheckCount());
				ProsOpinionInfo prosa = surveyResultsMapper.selectProsOpinionInfoByPreviousStateGuid(pros);
				if(!StringUtils.isNull(prosa) && !StringUtils.isEmpty(prosa.getOperateDate()) && !StringUtils.isEmpty(poi.getOperateDate())) {
					poi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(prosa.getOperateDate()), DateUtil.parseDateTime(poi.getOperateDate()), DateUnit.DAY)));
				}else {
					poi.setCauseContent("0");
				}
			}else if("技术负责人审查通过".equals(poi.getRole()) || "技术负责人审查退回".equals(poi.getRole())) {
				if("整改后直接使用".equals(poi.getState())) {
					ProsOpinionInfo pros = new ProsOpinionInfo();
					pros.setDataGuid(dataGuid);
					pros.setGuid(poi.getGuid());
					pros.setCheckCount(poi.getCheckCount());
					ProsOpinionInfo prosa = surveyResultsMapper.selectProsOpinionInfoByPreviousStateGuid(pros);
					if(!StringUtils.isNull(prosa) && !StringUtils.isEmpty(prosa.getOperateDate()) && !StringUtils.isEmpty(poi.getOperateDate())) {
						poi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(prosa.getOperateDate()), DateUtil.parseDateTime(poi.getOperateDate()), DateUnit.DAY)));
					}else {
						poi.setCauseContent("0");
					}
				}else {
					ProsOpinionInfo pros = new ProsOpinionInfo();
					pros.setDataGuid(dataGuid);
					pros.setCheckCount(poi.getCheckCount());
					String operateDate = surveyResultsMapper.selectProsOpinionInfoMaxTime(pros);
					if(!StringUtils.isEmpty(operateDate) && !StringUtils.isEmpty(poi.getOperateDate())) {
						poi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(operateDate), DateUtil.parseDateTime(poi.getOperateDate()), DateUnit.DAY)));
					}else {
						poi.setCauseContent("0");
					}
				}
			}else if("技术性整改上报".equals(poi.getRole())) {
				ProsOpinionInfo pros = new ProsOpinionInfo();
				pros.setDataGuid(dataGuid);
				pros.setRole("技术负责人审查退回");
				ProsOpinionInfo prosa = surveyResultsMapper.selectProsOpinionInfoByPreviousStateGuid(pros);
				if(!StringUtils.isNull(prosa) && !StringUtils.isEmpty(prosa.getOperateDate()) && !StringUtils.isEmpty(poi.getOperateDate())) {
					poi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(prosa.getOperateDate()), DateUtil.parseDateTime(poi.getOperateDate()), DateUnit.DAY)));
				}else {
					poi.setCauseContent("0");
				}
			}else if("整改上报退回".equals(poi.getRole())) {
				ProsOpinionInfo result = rectReportReturn(poiList,poi);
				if(StringUtils.isNotNull(result)) {
					poi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(result.getOperateDate()), DateUtil.parseDateTime(poi.getOperateDate()), DateUnit.DAY)));
				}else {
					poi.setCauseContent("0");
				}
			}else {
				poi.setCauseContent("0");
			}
		}
		return poiList;
	}

	@Override
	public ProReviewPassRefuse ProReviewPassRefuseDetail(String guid) {
		ProsOpinionInfo poi = surveyResultsMapper.selectProsOpinionInfoByGuid(guid);
		ProReviewPassRefuse prpr = new ProReviewPassRefuse();
		prpr.setCauseContent(poi.getCauseContent());
		OpinionCauseContent occ = new OpinionCauseContent();
		occ.setNo("No");
		occ.setName("Name");
		occ.setValue("value");
		String nodePath = "/PsDataPacket/FDataList/ProceduralCheakOpininon";
		List<OpinionCauseContent> occList = XmlReadUtil.getOpinionCauseContentXml(poi.getOpinionConent(), nodePath, occ);
		prpr.setProceduralCheakOpininon(occList);
		return prpr;
	}

	@Override
	public ProsOpinionInfo selectProsOpinionInfoByGuid(String guid) {
		// TODO Auto-generated method stub
		return surveyResultsMapper.selectProsOpinionInfoByGuid(guid);
	}
	
	/**
	 * @Title: rectReportReturn
	 * @Description: role等于整改上报退回，指定审查人员时间间隔处理方法
	 * @Author wtj
	 * @DateTime 2020-9-17 10:37:19
	 * @param poiList  根据dateGuid查询出的集合
	 * @param opinion  role等于整改上报退回查询出的实体
	 * @return
	 */
	private ProsOpinionInfo rectReportReturn(List<ProsOpinionInfo> poiList,ProsOpinionInfo opinion) {
		ProsOpinionInfo result = null;
		for(ProsOpinionInfo item:poiList) {
			if(DateUtil.between(DateUtil.parse(opinion.getOperateDate()), DateUtil.parse(item.getOperateDate()), DateUnit.MS, false)<0) {
				if(result != null) {
					if(DateUtil.between(DateUtil.parse(result.getOperateDate()), DateUtil.parse(item.getOperateDate()), DateUnit.MS, false)>0) {
						result = item;
					}
				}else {
					result = item;
				}
			}
		}
		return result;
	}

	@Override
	public List<ProsOpinionInfo> selectProsOpinionInfoByPGuid(String pGuid) {
		// TODO Auto-generated method stub
		return surveyResultsMapper.selectProsOpinionInfoByPGuid(pGuid);
	}

}
